<template>
  <el-drawer
    title="发送通知"
    :append-to-body="true"
    :close-on-press-escape="false"
    size="540px"
    :visible.sync="drawer"
  >
    <el-container>
      <el-main>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-select v-model="value" filterable placeholder="请选择" prop="template_id">
            <el-option
              v-for="item in gettemplates"
              :key="item.template_id"
              :label="item.title"
              :value="item.template_id"
            ></el-option>
          </el-select>
        </el-form>
        <message
          v-if="value"
          v-bind="{setdata, message: gettemplates.find(v=> v.template_id === value)}"
        ></message>
        <ul class="list list--picture-card">
          <li
            v-for="(v, i) in getfans.data.filter(v1=> users.find(v2=> v2 === v1.openid))"
            :key="`${i}sendmessage1`"
            class="list__item"
          >
            <img :src="v.headimgurl" class="list__item-thumbnail" alt />
            <div class="list__item-actions">
              <i @click="setselectUser(v.openid)" class="el-icon-delete"></i>
            </div>
          </li>
        </ul>
      </el-main>
      <el-footer>
        <el-button type="primary" @click="send">发送</el-button>
      </el-footer>
    </el-container>
  </el-drawer>
</template>

<script>
import message from "./message.vue";
export default {
  name: "sendmessage",
  data() {
    return {
      value: "",
      ruleForm: {
        template_id: "",
        url: "",
        data: {}
      },
      rules: {
        template_id: [{ required: true, message: "选择模版", trigger: "blur" }]
      }
    };
  },
  methods: {
    setdata(data) {
      this.ruleForm = data;
    },
    async de() {
      return new Promise(r => {
        setTimeout(r(), 1000);
      });
    },
    async send() {
      this.$refs["ruleForm"].validate(async valid => {
        if (valid) {
          const { template_id, url, data } = this.ruleForm;
          const fdata = {
            template_id,
            url,
            data
          };
          for (let v of this.users) {
            fdata.touser = v;
            await this.messagesend(fdata);
            this.setselectUser(v);
          }
          this.$alert("发送完成");
          this.setdrawer(false);
        } else {
          return false;
        }
      });
    }
  },
  props: {
    drawer: {
      type: Boolean
    },
    setdrawer: {
      type: Function
    },
    users: {
      type: Array,
      required: true
    },
    gettemplates: {
      type: Array,
      required: true
    },
    messagesend: {
      type: Function,
      required: true
    },
    getfans: {
      type: Object
    },
    setselectUser: {
      type: Function
    }
  },
  components: {
    message
  }
};
</script>

<style scoped>
.list--picture-card {
  margin: 0;
  display: inline;
  vertical-align: top;
}
.list {
  margin: 0;
  padding: 0;
  list-style: none;
}
.list__item {
  overflow: hidden;
  background-color: #fff;
  border: 1px solid #c0ccda;
  border-radius: 6px;
  box-sizing: border-box;
  width: 48px;
  height: 48px;
  margin: 0 8px 8px 0;
  display: inline-block;
  transition: all 0.5s cubic-bezier(0.55, 0, 0.1, 1);
  font-size: 14px;
  color: #606266;
  line-height: 1.8;
  position: relative;
}

.list__item-actions:hover {
  opacity: 0.9;
}

.list__item-thumbnail {
  width: 100%;
  height: 100%;
}
.list__item-actions {
  position: absolute;
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
  cursor: default;
  text-align: center;
  color: #fff;
  opacity: 0;
  font-size: 20px;
  background-color: rgba(0, 0, 0, 0.5);
  transition: opacity 0.3s;
  cursor: pointer;
}
</style>